package javaweb_work01.main.Servlet;

import javaweb_work01.main.Entity.Students;
import javaweb_work01.main.Service.*;
import javaweb_work01.main.Utils.PageContrl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 增加学员功能的Servlet类
 */
@WebServlet(name = "AddServlet", urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决POST接收乱码
        request.setCharacterEncoding("utf-8");
        //得到输入的数据
        int num = Integer.parseInt(request.getParameter("num"));
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String date = request.getParameter("date");
        String email = request.getParameter("email");
        String remark = request.getParameter("remark");
        int classId = Integer.parseInt(request.getParameter("classId"));
        //得到CheckService的对象用于调用检查方法
        CheckService checkService = new CheckService();
        //返回Students类对象的集合，即查询出来的学员信息
        List<Students> checkStudents = checkService.checkStudentsService(num);
        //如果集合中有内容，证明该学号已存在
        if(checkStudents.size() != 0){
            //添加属性值，用于增加学员页面的显示
            request.setAttribute("numError","学号已存在!");
            //服务器内跳转回增加学员页面
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("add.jsp");
            //转发对象
            requestDispatcher.forward(request,response);
        }else {
            //调用增加学员数据方法
            AddService addService = new AddService();
            //通过得到的数据构造新的Students对象并输入数据库
            int i = addService.addStudentsService(new Students(num, name, sex, date, email, remark, classId));
            //得到返回值并判断，返回值为表中改变的数据，若返回值不为0则添加成功
            if (i != 0) {
                //获取主页需要显示的信息，构建主页
                //声明CountService类的对象，以便调用方法
                CountService countService = new CountService();
                //调用Service中的方法，最后调用Dao层方法，返回数据库中的数据总数，即学员总数
                int count = countService.countStudentsService();
                //利用得到的学员总数构造新的PageContrl对象，用于分页显示，此处为从第0条开始，显示10条
                PageContrl pageContrl = new PageContrl(0,10,count);
                //声明ShowService类的对象
                ShowService showService = new ShowService();
                //利用ShowService类的对象调用方法，最后调用Dao层方法，返回数据库中该页需要显示的学员信息
                List<Students> students = showService.showStudentsService(pageContrl);

                //获取当前所有班级编号
                //调用Service层方法，进入Dao层并得到班级编号结果集
                CheckClassesIdservice checkClassesIdservice = new CheckClassesIdservice();
                List<Object[]> objects = checkClassesIdservice.checkClassesIdService();
                //准备一个集合用于存放结果集中的班级编号
                ArrayList<Integer> allClassesId = new ArrayList<>();
                for(Object[] j:objects){
                    allClassesId.add((Integer) j[0]);
                }

                //将得到的PageContrl对象，Students对象，班级编号集合放入session中实现共享
                request.getSession().setAttribute("pageContrl",pageContrl);
                request.getSession().setAttribute("showStudents",students);
                request.getSession().setAttribute("allClassesId",allClassesId);
                //跳转到主页
                response.sendRedirect("main.jsp");
            //若添加失败
            } else {
                System.out.println("添加失败");
                //服务器内跳转回增加学员页面
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("add.jsp");
                //转发对象
                requestDispatcher.forward(request, response);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
